package com.eightbears.beboy.base.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Style;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.eightbears.beboy.R;


public class GoodProgressView extends View {

    private int[] mColors = {Color.parseColor("#d3b0fe"), Color.parseColor("#89b5fc")};//进度条颜色（渐变色的2个点）
    private int backgroundColor = Color.parseColor("#d7e9ff");//进度条默认颜色
    private int[] curValue = {100, 900, 4000, 25000};
    private int textColor = Color.GRAY;//文本颜色
    private Paint mPaint;//画笔

    private Paint textPaint;
    private Paint textSelPaint;

    private String level_1 = getContext().getString(R.string.text_level_1);
    private String level_2 = getContext().getString(R.string.text_level_2);
    private String level_3 = getContext().getString(R.string.text_level_3);
    private String level_4 = getContext().getString(R.string.text_level_4);
    private String level_5 = getContext().getString(R.string.text_level_5);

    private Bitmap level_1_img = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_driver_student);
    private Bitmap level_2_img = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_greenhand_plan);
    private Bitmap level_3_img = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_driver_model);
    private Bitmap level_4_img = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_old_driver);
    private Bitmap level_5_img = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_god);


    private int progressValue = 0;//进度值
    private int mLevel = 1;
    private int mPoint = 10;
    private float mViewWhite;
    private float curWidth = 0;
    private int lineMargin = 55;

    private int textTopMargin = 20;

    public GoodProgressView(Context context) {
        this(context, null);
    }

    public GoodProgressView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    // 获得我自定义的样式属性
    public GoodProgressView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mPaint = new Paint();
        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textSelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        progressValue = 0;
        // 获得我们所定义的自定义样式属性
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.GoodProgressView, defStyle, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.GoodProgressView_gpv_startColor:
                    // 渐变色之起始颜色，默认设置为红色
                    mColors[0] = a.getColor(attr, Color.RED);
                    break;
                case R.styleable.GoodProgressView_gpv_endColor:
                    // 渐变色之结束颜色，默认设置为品红
                    mColors[1] = a.getColor(attr, Color.MAGENTA);
                    break;
                case R.styleable.GoodProgressView_gpv_bgColor:
                    // 进度条默认颜色，默认设置为灰色
                    backgroundColor = a.getColor(attr, Color.GRAY);
                    break;
                case R.styleable.GoodProgressView_gpv_textColor:
                    // 文字颜色，默认设置为灰色
                    textColor = a.getColor(attr, Color.GRAY);
                    break;
            }
        }
        a.recycle();
    }


    private void initLevelInfo() {
        textPaint.setColor(getResources().getColor(R.color.text_color_ccc));
        textPaint.setTextSize(25);
        textSelPaint.setColor(getResources().getColor(R.color.text_select_color));
        textSelPaint.setTextSize(26);

        if (mLevel == 1) {
            level_1_img = BitmapFactory.decodeResource(getResources(), R.mipmap.level_1_sel);
        } else if (mLevel == 2) {
            level_2_img = BitmapFactory.decodeResource(getResources(), R.mipmap.level_2_sel);
        } else if (mLevel == 3) {
            level_3_img = BitmapFactory.decodeResource(getResources(), R.mipmap.level_3_sel);
        } else if (mLevel == 4) {
            level_4_img = BitmapFactory.decodeResource(getResources(), R.mipmap.level_4_sel);
        } else if (mLevel == 5) {
            level_5_img = BitmapFactory.decodeResource(getResources(), R.mipmap.level_5_sel);
        }
    }

    public void setProgressValue(int progressValue) {
        if (progressValue > 100) {
            progressValue = 100;
        }
        this.progressValue = progressValue;
        Log.i("customView", "log: progressValue=" + progressValue);
    }

    public void setColors(int[] colors) {
        mColors = colors;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int width = 0;
        int height = 0;
        /**
         * 设置宽度
         */
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        switch (specMode) {
            case MeasureSpec.EXACTLY:// 明确指定了
                width = specSize;
                break;
            case MeasureSpec.AT_MOST:// 一般为WARP_CONTENT
                width = getPaddingLeft() + getPaddingRight();
                break;
        }

        /**
         * 设置高度
         */
        specMode = MeasureSpec.getMode(heightMeasureSpec);
        specSize = MeasureSpec.getSize(heightMeasureSpec);
        switch (specMode) {
            case MeasureSpec.EXACTLY:// 明确指定了
                height = specSize;
                break;
            case MeasureSpec.AT_MOST:// 一般为WARP_CONTENT
                height = width / 10;
                break;
        }
        setMeasuredDimension(width, height);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //初始化
        initLevelInfo();

        int width = getMeasuredWidth();
        int height = getMeasuredHeight();

        mViewWhite = (width - lineMargin * 2) / 4; // 每个等级屏幕宽度
        float curX = getCurX(mLevel) + lineMargin;

        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(10);
        mPaint.setStyle(Style.STROKE);
        mPaint.setStrokeCap(Cap.ROUND);
        mPaint.setColor(Color.TRANSPARENT);
        float offsetHeight = height / 2;                    //进度条高度
        float outerCircleDiameter = height / 4;             // 圆形半径

        int count = mColors.length;
        int[] colors = new int[count];
        System.arraycopy(mColors, 0, colors, 0, count);

        //底部灰色背景，指示进度条总长度//背景颜色
        mPaint.setShader(null);
        mPaint.setColor(backgroundColor);
        canvas.drawLine(lineMargin, offsetHeight, width - lineMargin, offsetHeight, mPaint);

        //设置渐变色区域
        int delCirclePoin = width - lineMargin - height * 2;
        LinearGradient shader = new LinearGradient(lineMargin, 0, delCirclePoin, 0, colors, null, Shader.TileMode.CLAMP);
        mPaint.setShader(shader);
        canvas.drawLine(lineMargin, offsetHeight, curX, offsetHeight, mPaint);

        //渐变色外圆
        mPaint.setStrokeWidth(1);
        mPaint.setStyle(Style.FILL);
        canvas.drawCircle(curX - (outerCircleDiameter / 4.8f), offsetHeight, outerCircleDiameter / 3, mPaint);

        // 等级描述 和 等级图片

        // level 1
        canvas.drawText(level_1, 0, height - textTopMargin, mLevel == 1 ? textSelPaint : textPaint);
        canvas.drawBitmap(level_1_img, 0, (offsetHeight - (level_1_img.getHeight() / 2) - (level_1_img.getHeight() / 5)), textPaint);
        // level 2
        canvas.drawText(level_2, mViewWhite - 10, height - textTopMargin, mLevel == 2 ? textSelPaint : textPaint);
        canvas.drawBitmap(level_2_img, mViewWhite, (offsetHeight - (level_2_img.getHeight() / 2) - (level_2_img.getHeight() / 5)), textPaint);
        // level 3
        canvas.drawText(level_3, mViewWhite * 2 - textTopMargin, height - textTopMargin, mLevel == 3 ? textSelPaint : textPaint);
        canvas.drawBitmap(level_3_img, mViewWhite * 2, (offsetHeight - (level_3_img.getHeight() / 2) - (level_3_img.getHeight() / 5)), textPaint);
        // level 4
        canvas.drawText(level_4, mViewWhite * 3 - textTopMargin, height - textTopMargin, mLevel == 4 ? textSelPaint : textPaint);
        canvas.drawBitmap(level_4_img, mViewWhite * 3, (offsetHeight - (level_4_img.getHeight() / 2) - (level_4_img.getHeight() / 5)), textPaint);
        // level 5
        canvas.drawText(level_5, mViewWhite * 4 - textTopMargin, height - textTopMargin, mLevel == 5 ? textSelPaint : textPaint);
        canvas.drawBitmap(level_5_img, mViewWhite * 4 + 15, (offsetHeight - (level_5_img.getHeight() / 2) - (level_5_img.getHeight() / 5)), textPaint);
    }


    /**
     * 计算当前坐标
     */
    private float getCurX(int level) {
        level = level == 5 ? 4 : level;
        curWidth = ((float) mPoint) / (((float) curValue[level - 1])) * mViewWhite;
        return curWidth + (level - 1) * mViewWhite;
    }

    public void setLevel(int level) {
        this.mLevel = level;
    }

    public void setPoints(int point) {
        this.mPoint = point;
    }
}
